function [] = assembleHistoneDS()
% Assemble a local representation of the histone modification data for GM06990 cell line (chrm 7), accessed from UCSC Encode DAS server
%
% assembleHistoneDS() retrieve histone modification data available for 
%                     GM06990 cell line, chromosome 7, from UCSC Encode DAS server using 
%                     DASMiner API. The function organizes this info in the 
%                     'histoneChips' data structure, and save it on GM06990chips_chrm7.mat 
%
% 
% SYNTAX: [] = assembleHistoneDS()
%
% See also assembleHistoneDSforClustering(),
% performHistoneClustering_Fig4CD()
%  
%    DASMiner: DAS library and browser for Matlab.
%    Diogo Veiga, Jan 2009.


%Entry point for the data structure that holds histone data
%holds histone modification data for all chips
histoneChips = struct;


%Data for GM06990 cell line, 
histoneChips.chips(1) = parseHistoneUCSCData('encodeSangerChipH3K4me1');
histoneChips.chips(2) = parseHistoneUCSCData('encodeSangerChipH3K4me2');
histoneChips.chips(3) = parseHistoneUCSCData('encodeSangerChipH3K4me3');
histoneChips.chips(4) = parseHistoneUCSCData('encodeSangerChipH3K27me3');
histoneChips.chips(5) = parseHistoneUCSCData('encodeSangerChipH3K36me3');
histoneChips.chips(6) = parseHistoneUCSCData('encodeSangerChipH3K79me3');
histoneChips.chips(7) = parseHistoneUCSCData('encodeSangerChipH3K9me3');
histoneChips.chips(8) = parseHistoneUCSCData('encodeSangerChipH3ac');
histoneChips.chips(9) = parseHistoneUCSCData('encodeSangerChipH4ac');
histoneChips.chips(10) = parseHistoneUCSCData('encodeSangerChipCTCF');

save GM06990chips_chrm7.mat histoneChips;


function [chip] = parseHistoneUCSCData(chipLabel)

global chip;
%global nChip;
chip = struct('label',{},'values',{}, 'sample', {}, ...
    'region', struct ('label', {},'chrm', {},'start',{},'stop', {}) );

%[r,nChip] = size(chip);

%chip(nChip+1).label = chipLabel;
chip(1).label = chipLabel;

% Encode data covering chrm 7
readEncodeRegions('7','115404472','117281897',chipLabel); % ENm001 chr7:115,404,472-117,281,897
readEncodeRegions('7','26730761','27230760',chipLabel); % ENm010 chr7:26,730,761-27,230,760
readEncodeRegions('7','113527084','114527083',chipLabel); % ENm012 chr7:113,527,084-114,527,083
readEncodeRegions('7','89428340','90542763',chipLabel); % ENm013 chr7:89,428,340-90,542,763
readEncodeRegions('7','125672607','126835803',chipLabel); % ENm014 chr7:125,672,607-126,835,803


function readEncodeRegions(chrm,start,stop,chipLabel)

global chip;
%global nChip;
[xmlS, xml] = executeDASCommand('http://hgwdev-gencode.cse.ucsc.edu/cgi-bin/das','command', 'features', 'DSN', 'hg18','chrom',chrm,'start',start ...
    , 'stop', stop, 'featuresList',{chipLabel});


nFeatures = size(xmlS.GFF{1}.SEGMENT{1}.FEATURE,2);
values = zeros(nFeatures,1);
nRegions = size(chip(1).region,2);

for i=1:nFeatures
    
    %chipLabel = xmlS.GFF{1}.SEGMENT{1}.FEATURE{i}.METHOD{1}.CONTENT;
    values(i) = str2double(xmlS.GFF{1}.SEGMENT{1}.FEATURE{i}.SCORE{1}.CONTENT);
    chip(1).region(i+nRegions).chrm = '7';
    chip(1).region(i+nRegions).start = xmlS.GFF{1}.SEGMENT{1}.FEATURE{i}.START{1}.CONTENT;
    chip(1).region(i+nRegions).stop = xmlS.GFF{1}.SEGMENT{1}.FEATURE{i}.END{1}.CONTENT;
end

chip(1).values = [chip(1).values ; values];
chip(1).sample = ['GM06990' chipLabel(17:end)]; %cell line and histone modification










